package com.iwhalecloud.bss.kite.cucc.service.custview;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewBaseVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.Fee;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.FeeInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.HistoryFeeInfoVO;
import com.iwhalecloud.bss.kite.cucc.service.custview.evolve.task.FeeInfoTask;
import com.iwhalecloud.bss.kite.cucc.service.custview.evolve.task.GroupInfoTask;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.ztesoft.zsmart.core.log.ZSmartLogger;

/**
 * @ClassName : GetFeeInfoTask
 * @Description : 资费信息
 * @Author : xie.zhengjun
 * @Date : 2021/1/23 13:00
 * @deprecated 已停止维护，请勿使用。改进版本解除Callable绑定，干干净净，只专注业务本身。见：{@link FeeInfoTask}
 */
public class GetFeeInfoTask extends CustViewBaseTask {
    private final ZSmartLogger LOGGER = ZSmartLogger.getLogger(GetFeeInfoTask.class);
    public GetFeeInfoTask(Object[] args) {
        super(args);
    }

    @Override
    public CustViewBaseVO call() throws Exception {
        Object[] args = getArgs();
        if (args.length != 2) {
            return null;
        }
        Map<String, Object> userDetail = (Map<String, Object>) args[0];
        CustViewVO custViewVO = (CustViewVO) args[1];

        FeeInfoVO feeInfoVO = new FeeInfoVO();
        HistoryFeeInfoVO historyFeeInfoVO = new HistoryFeeInfoVO();
        custViewVO.setFee(feeInfoVO);
        custViewVO.setHistoryFee(historyFeeInfoVO);

        //历史资费信息
        List<Fee> historyFeeList = new ArrayList<>();
        List<Fee> feeList = new ArrayList<>();

        if (KiteMapUtils.isNotEmpty(userDetail)) {
            List<Map<String, Object>> userDiscntInfos = (List<Map<String, Object>>) userDetail.get("USER_DISCNT_INFOS");
            if (KiteListUtils.isNotEmpty(userDiscntInfos)) {
                userDiscntInfos.forEach(userDiscntInfoMap -> {
                    Fee fee = new Fee();
                    fee.setFeeCode(KiteMapUtils.getString(userDiscntInfoMap, "DISCNT_CODE"));
                    // 资费名称
                    String feeName = "";
                    String feeDesc = "详情";
                    String discntProductId = KiteMapUtils.getString(userDiscntInfoMap, "DISCNT_PRODUCT_ID");
                    if (KiteStringUtils.isNotEmpty(discntProductId)) {
                        Offer additionalOffer = OfferCache.get(discntProductId);
                        if (additionalOffer != null) {
                            feeName = additionalOffer.getOfferName();
                        }
                    }

                    fee.setFeeName(feeName);
                    fee.setFeeDesc(feeDesc);

                    //资费属性
                    List<Map<String, Object>> userDiscntItemInfos = (List<Map<String, Object>>) userDiscntInfoMap.get("USER_DISCNT_ITEM_INFOS");
                    if (KiteListUtils.isNotEmpty(userDiscntItemInfos)) {
                        Map<String, String> attrMap = getAttrMap(userDiscntItemInfos);
                        fee.setAttrMap(attrMap);
                    }

                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    String startDate = KiteMapUtils.getString(userDiscntInfoMap, "START_DATE");
                    String endDate = KiteMapUtils.getString(userDiscntInfoMap, "END_DATE");
                    Date start = null;
                    Date end = null;
                    try {
                        start = simpleDateFormat.parse(startDate);
                        end = simpleDateFormat.parse(endDate);
                    } catch (ParseException e) {
                        LOGGER.error(e);
                        ErrorConsts.BUSI_COMMON_ERROR.throwOut("格式化时间失败");
                    }

                    if (end != null && start != null) {
                        fee.setStartDate(KiteDateUtils.formatDate(start, "yyyy-MM-dd"));
                        fee.setEndDate(KiteDateUtils.formatDate(end, "yyyy-MM-dd"));
                        // 失效时间大于当日，则认为是资费信息；失效时间小于当日，则认为是历史资费信息。
                        long currentTimeMillis = KiteDateUtils.getCurrentTimeMillis();

                        long time = end.getTime();
                        if (currentTimeMillis > time) {
                            //历史资费信息
                            historyFeeList.add(fee);
                        } else {
                            //资费信息
                            feeList.add(fee);
                        }
                    }
                });

                feeInfoVO.setFees(feeList);
                historyFeeInfoVO.setFees(historyFeeList);
            }
        }
        return feeInfoVO;
    }

    /**
     * 翻译资费属性
     *
     * @param userDiscntItemInfos 资费属性map
     * @return 翻译后的资费属性map
     */
    public static Map<String, String> getAttrMap(List<Map<String, Object>> userDiscntItemInfos) {
        Map<String, String> attrMap = new HashMap<>();
        for (Map<String, Object> userDiscntItemInfo : userDiscntItemInfos) {
            String crmAttrCode = KiteMapUtils.getString(userDiscntItemInfo, "CRM_ATTR_CODE");
            String crmAttrValue = KiteMapUtils.getString(userDiscntItemInfo, "CRM_ATTR_VALUE");

            String attrValue = "";
            switch (crmAttrCode) {
                case "200000106":
                    //产品编码
                    attrMap.put("产品编码", crmAttrValue);
                    break;
                case "200000133":
                    //协议资费
                    attrMap.put("协议资费", crmAttrValue);
                    break;
                case "200000169":
                    //协议月租（元）
                    attrMap.put("协议月租（元）", crmAttrValue);
                    break;
                case "200000170":
                    //首月收取方式
                    attrValue = Optional.ofNullable(AttrValueCache.getAttrValue("202012150004", crmAttrValue))
                        .map(AttrValue::getAttrValueName).orElse(crmAttrValue);
                    attrMap.put("首月收取方式", attrValue);
                    break;
                case "200000171":
                    //末月收取方式
                    attrValue = Optional.ofNullable(AttrValueCache.getAttrValue("202012150005", crmAttrValue))
                        .map(AttrValue::getAttrValueName).orElse(crmAttrValue);
                    attrMap.put("末月收取方式", attrValue);
                    break;
                case "200000172":
                    //生效时间
                    attrMap.put("生效时间", crmAttrValue);
                    break;
                case "200000173":
                    //失效时间
                    attrMap.put("失效时间", crmAttrValue);
                    break;
                case "200000174":
                    //生效模式
                    attrValue = Optional.ofNullable(AttrValueCache.getAttrValue("202012150224", crmAttrValue))
                        .map(AttrValue::getAttrValueName).orElse(crmAttrValue);
                    attrMap.put("生效模式", attrValue);
                    break;
                case "200000185":
                    //包编码
                    attrMap.put("包编码", crmAttrValue);
                    break;
                /*case "200001735":
                    //失效模式
                    attrMap.put("失效模式", crmAttrValue);
                    break;*/
                case "200001354":
                    //标准月租（元）
                    attrMap.put("标准月租（元）", crmAttrValue);
                    break;
                case "200002509":
                    //价格（元）
                    attrMap.put("价格（元）", crmAttrValue);
                    break;
                default:
                    break;
            }
        }
        return attrMap;
    }
}
